package com.cold.demo.generator.mybatis;

import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.Context;
import org.mybatis.generator.config.TableConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zyc
 */
@SpringBootApplication
@Controller
@WebFilter
public class BuilderApplication implements Filter {

    /**
     * SpringApplication.run(BuilderApplication.class, args);
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
//        SpringApplication.run(BuilderApplication.class, args);
        createCode("config/generatorConfig-explain.xml");
    }

    @GetMapping("/index")
    @ResponseBody
    public Object index(HttpSession session, HttpServletRequest request){
        System.out.println(session);
        System.out.println(session.getId());
        System.out.println(request);
        System.out.println(request.getRequestedSessionId());
        return "";
    }

    /**
     * 一键从数据库的表中生成代码
     * @throws Exception
     */
    public static void createCode(String generatorConfigPath)throws Exception{
        String projectRootPath = new File("").getCanonicalPath();
        GeneratorCreator generatorCreator = new GeneratorCreator();
        //获取配置文件路径
        String xmlPath = BuilderApplication.class.getClassLoader().getResource(generatorConfigPath).getFile().substring(1);
        //生成相关的 entity+Mapper+XML 代码并返回配置对象
        Configuration config = generatorCreator.createCode(xmlPath);
        Context context = config.getContexts().get(0);
        //从配置信息中获取对应本次生成的表名字
        List<String> tableNames = context.getTableConfigurations().stream().map(TableConfiguration::getTableName).collect(Collectors.toList());
        System.out.println("本次生成的表名:"+tableNames);
        //通过配置对象获取 groupId
        String javaModelTargetPackage = context.getJavaModelGeneratorConfiguration().getTargetPackage();
        String basePackage = javaModelTargetPackage.substring(0,javaModelTargetPackage.lastIndexOf("."));
        System.out.println("本次生成代码的basePackage:"+basePackage);
        //根据 groupId 的值生成对呀的 Controller+Service+ServiceImpl
        CrudCreator crudCreator = new CrudCreator(tableNames,basePackage,projectRootPath);
        crudCreator.creator();
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println(((HttpServletRequest)servletRequest).getServletPath());
        filterChain.doFilter(servletRequest, servletResponse);
    }
}
